package com.cjq.goods.service.impl;

import com.cjq.common.domain.Classify;
import com.cjq.common.domain.Goods;
import com.cjq.common.domain.request.GoodsReq;
import com.cjq.common.domain.response.GoodsRes;
import com.cjq.common.result.PageResult;
import com.cjq.common.result.Result;
import com.cjq.goods.mapper.GoodsMapper;
import com.cjq.goods.service.GoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public Result<Goods> findById(Long id) {
        Goods goods = goodsMapper.findById(id);
        return Result.success(goods);
    }

    @Override
    public Result<PageResult<GoodsRes>> goodsList(GoodsReq goodsReq) {
        PageHelper.startPage(goodsReq.getPageNum(),goodsReq.getPageSize());
        List<GoodsRes> goodsResList = goodsMapper.goodsList(goodsReq);
        PageInfo<GoodsRes> info = new PageInfo<>(goodsResList);
        Result<PageResult<GoodsRes>> result = PageResult.toResult(info.getTotal(), goodsResList);
        return result;
    }

    @Override
    public Result<List<Classify>> classifyList() {
        List<Classify> classifyList = goodsMapper.classifyList();
        return Result.success(classifyList);
    }

    @Override
    public Result save(Goods goods) {
        goodsMapper.save(goods);
        return Result.success();
    }

    @Override
    public Result update(Goods goods) {
        goodsMapper.update(goods);
        return Result.success();
    }
}
